package cn.kongwg.boot.controller;

import cn.kongwg.boot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

/**
 * @author kongwg
 * @create 2021-03-14 22:27
 */
@Controller
public class IndexController {


    @GetMapping({"/", "/login"})
    public String goLogin() {
        return "login";
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model) {


        if (StringUtils.hasLength(user.getUserName()) && "123".equals(user.getPassword())) {
            session.setAttribute("loginUser", user);
            return "redirect:/main.html";
        } else {
            model.addAttribute("msg", "账号密码错误");
            return "login";

        }

    }


    @GetMapping("/main.html")
    public String mainPage() {
       /* User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser != null) {
            String userName = loginUser.getUserName();
            model.addAttribute("username", userName);
            return "main";

        } else {
            model.addAttribute("msg", "请登录");


            return "login";
        }*/
        return "main";

    }
}
